<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import echarts from 'echarts'
  import { getDay30 } from '@/api/other/index'

  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '400px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null,
        data: []
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        getDay30().then(response => {
          this.data = response.data
          this.setOptions( this.data)
        })
      },
      setOptions(data) {
        var xdata=[],ydata=[];
        for (var i=0;i<data.length;i++){
          xdata.push(data[i].month)
          ydata.push(data[i].truckAccessCount)
        }
        this.chart.setOption({
          xAxis: {
            data: xdata,
            boundaryGap: true,
            axisTick: {
              show: true
            }
          },
          grid: {
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            }
          },
          legend: {
            data: [ '运输车辆通行次数']
          },
          series: [{
            name: '运输车辆通行次数',
            smooth: true,
            type: 'bar',
            barWidth : 30,//柱图宽度
            data: ydata,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }]
        })
      }
    }
  }
</script>
